package ATM.dao.impl;

import ATM.dao.BaseDAO;
import ATM.dao.UserDAO;
import ATM.pojo.UserDO;

import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public UserDO getByUsernameAndPwd(String name, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_usename=? and u_usepsw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setUserName(rs.getString("u_usename"));
                user.setUserPassWord(rs.getString("u_usepsw"));
                user.setId(rs.getInt("u_id"));
                user.setMoney(rs.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getById(int id) {
        this.con = getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setUserName(rs.getString("u_usename"));
                user.setUserPassWord(rs.getString("u_usepsw"));
                user.setId(rs.getInt("u_id"));
                user.setMoney(rs.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getByName(String name)  {
        this.con=getConnection();
        statement=null;
        rs=null;
        UserDO user =null;
        String sql = "select * from t_user where u_usename=?";
        try {
            con.prepareStatement(sql);
            statement.setObject(1,name);
            rs=statement.executeQuery();
            if (rs.next()){
                user = new UserDO();
                user.setUserName(rs.getString("u_usename"));
                user.setUserPassWord(rs.getString("u_usepsw"));
                user.setId(rs.getInt("u_id"));
                user.setMoney(rs.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement,rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        this.getConnection();
        String sql = "update t_user set u_usepsw=?,u_money=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getUserPassWord());
            statement.setObject(2, user.getMoney());
            statement.setObject(3, user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, statement);
        }

    }
}
